package com.spi.soconversion.base.definition;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

import com.spi.soconversion.util.SOCConstants;
import com.spi.soconversion.util.SOCUtility;
import com.spi.soconversion.util.XMLAttributeConstants;
import com.spi.soconversion.util.XMLTagConstants;

public class SOCOrder {

	private String billToID;
	private String buyerUserID;
	private String bilToKey;
	private String shipToKey;
	private String headerShipToOrgId;
	private String headerShipToKey;
	private String createdAtNode=SOCConstants.FLAG_Y;
	private String custContactID;
	private String custEmailID;
	private String documentType=SOCConstants.CLOSED_ORDER_DOCUMENT_TYPE;
	private String enteredBy;
	private String enterpriseCode=SOCConstants.BAY;
	private String orderComplete=SOCConstants.FLAG_Y;
	private String orderDate;
	private String orderHeaderKey=SOCConstants.EMPTY_STRING;
	private String orderName=SOCConstants.CLOSED_ORDER_NAME;
	private String orderNum;
	private String orgTax;
	private String orgTotalAmt;
	private String otherCharges;
	private String purgeHisDate=SOCConstants.EMPTY_STRING;
	private String sellerOrgCode;
	private String shipNode=SOCConstants.EMPTY_STRING;
	private String shipToID;
	private String taxExempt;
	private String taxJurisdiction=SOCConstants.EMPTY_STRING;
	private String vendorID=SOCConstants.EMPTY_STRING;
	private SOCOrderExtn orderExtn = new SOCOrderExtn();
	private SOCOrderNotes orderNotes = new SOCOrderNotes();
	private SOCOrderPriceInfo orderPriceInfo = new SOCOrderPriceInfo();
	private SOCOrderPersonalInfoBillTo infoBillTo = new SOCOrderPersonalInfoBillTo();
	private SOCOrderPersonalInfoShipTo infoShipTo = new SOCOrderPersonalInfoShipTo();
	private SOCOrderPersonalInfoSoldTo infoSoldTo = new SOCOrderPersonalInfoSoldTo();
	private SOCHeaderCharges headerCharges = new SOCHeaderCharges();
	private SOCOrderLines orderLines = new SOCOrderLines();
	private BigDecimal totalHeaderCharge=new BigDecimal("0");
	private BigDecimal totalTax=new BigDecimal("0");
	private BigDecimal totalLine=new BigDecimal("0");
	
	public String getBillToKey() {
		return  SOCUtility.checkNull(bilToKey);
	}

	public void setBillToKey(String bilToKey) {
		this.bilToKey = bilToKey;
	}

	public String getShipToKey() {
		return  SOCUtility.checkNull(shipToKey);
	}

	public void setShipToKey(String shipToKey) {
		this.shipToKey = shipToKey;
	}
	
	public String getHeaderShipToOrgId() {
		return headerShipToOrgId;
	}

	public void setHeaderShipToOrgId(String headerShipToOrgId) {
		this.headerShipToOrgId = headerShipToOrgId;
	}
	
	public String getBillToID() {
		return  SOCUtility.checkNull(billToID);
	}

	public void setBillToID(String billToID) {
		this.billToID = billToID;
	}

//	public String getBuyerUserId() {
//		return  SOCUtility.checkNull(buyerUserID);
//	}
//
//	public void setBuyerUserId(String buyerUserId) {
//		this.buyerUserID = buyerUserId;
//	}

	public String getCreatedAtNode() {
		return createdAtNode;
	}

	public String getCustContactID() {
		return  SOCUtility.checkNull(custContactID);
	}

	public void setCustContactID(String custContactID) {
		this.custContactID = custContactID;
	}
	
	public void setCustEmailID(String custEmailID) {
		this.custEmailID = custEmailID;
	}
	
	public String getCustEmailID() {
		
		custEmailID= SOCUtility.checkNull(custEmailID);
		if(custEmailID.length()>150)
		{
			custEmailID = custEmailID.substring(0, 150);
		}
		return custEmailID ;
	}

	public String getDocumentType() {
		return documentType;
	}

	public void setEnteredBy(String enteredBy) {
		this.enteredBy = enteredBy;
	}
	public String getEnteredBy() {
		return  SOCUtility.checkNull(enteredBy);
	}

	public String getEnterpriseCode() {
		return enterpriseCode;
	}

	public String getOrderComplete() {
		return orderComplete;
	}

	public void setOrderDate(String orderDate) {
		this.orderDate = orderDate;
	}

	public String getOrderDate() {
		return  SOCUtility.checkNull(orderDate);
	}

	public String getOrderHeaderKey() {
		return orderHeaderKey;
	}

	public void setOrderName(String orderName) {
		this.orderName = this.orderName+orderName;
	}

	public String getOrderName() {
		return orderName;
	}

	public void setOrderNum(String orderNum) {
		this.orderNum = orderNum;
	}

	public String getOrderNum() {
		return  SOCUtility.checkNull(orderNum);
	}

	public void setOrgTax(String orgTax) {
		this.orgTax = orgTax;
	}

	public String getOrgTax() {
		return  SOCUtility.checkNull(orgTax);
	}

	public void setOrgTotalAmt(String orgTotalAmt) {
		this.orgTotalAmt = orgTotalAmt;
	}

	public String getOrgTotalAmt() {
		return  SOCUtility.checkNull(orgTotalAmt);
	}

	public void setOtherCharges(String otherCharges) {
		this.otherCharges = otherCharges;
	}

	public String getOtherCharges() {
		return  SOCUtility.checkNull(otherCharges);
	}

	public String getPurgeHisDate() {
		return purgeHisDate;
	}

	public void setSellerOrgCode(String sellerOrgCode) {
		this.sellerOrgCode = sellerOrgCode;
	}

	public String getSellerOrgCode() {
		return  SOCUtility.checkNull(sellerOrgCode);
	}

	public String getShipNode() {
		return shipNode;
	}

	public void setShipToID(String shipToID) {
		this.shipToID = shipToID;
	}

	public String getShipToID() {
		return  SOCUtility.checkNull(shipToID);
	}

	public void setTaxExempt(String taxExempt) {
		this.taxExempt = taxExempt;
	}

	public String getTaxExempt() {
		return  SOCUtility.checkNull(taxExempt);
	}

	public String getTaxJurisdiction() {
		return taxJurisdiction;
	}

	public String getVendorID() {
		return vendorID;
	}
	
	public SOCOrderExtn getOrderExtn() {
		if(orderExtn!=null)
		{
			orderExtn.setExtnAmtCollected(otherCharges);
		}
		return orderExtn;
	}

	public void setOrderExtn(SOCOrderExtn orderExtn) {		
		this.orderExtn=orderExtn;
	}
	
	public SOCOrderNotes getOrderNotes() {
		return orderNotes;
	}

	public void setOrderNotes(SOCOrderNotes orderNotes) {
		this.orderNotes=orderNotes;
	}	

	public SOCOrderPriceInfo getOrderPriceInfo() {
		if(orderPriceInfo!=null)
		{
			orderPriceInfo.setTotalAmt(orgTotalAmt);
		}
		return orderPriceInfo;
	}

	public void setOrderPriceInfo(SOCOrderPriceInfo orderPriceInfo) {
		this.orderPriceInfo=orderPriceInfo;
	}
	
//	public SOCOrderPersonalInfoBillTo getInfoBillTo() {
//		return infoBillTo;
//	}
//
//	public void setInfoBillTo(SOCOrderPersonalInfoBillTo infoBillTo) {
//		this.infoBillTo=infoBillTo;
//	}
	
	public SOCOrderPersonalInfoShipTo getInfoShipTo() {
		return infoShipTo;
	}

	public void setInfoShipTo(SOCOrderPersonalInfoShipTo infoShipTo) {
		this.infoShipTo=infoShipTo;
	}
	
//	public SOCOrderPersonalInfoSoldTo getInfoSoldTo() {
//		return infoSoldTo;
//	}
//
//	public void setInfoSoldTo(SOCOrderPersonalInfoSoldTo infoSoldTo) {
//		this.infoSoldTo=infoSoldTo;
//	}	
//	
	public SOCHeaderCharges getHeaderCharges() {
		return headerCharges;
	}

	public void setHeaderCharges(SOCHeaderCharges headerCharges) {
		this.headerCharges=headerCharges;
	}	
	
	public SOCOrderLines getOrderLines() {
		return orderLines;
	}

	public void setOrderLines(SOCOrderLines orderLines) {
		this.orderLines=orderLines;
	}	
	
	public String getHeaderShipToKey() {
		return headerShipToKey;
	}

	public void setHeaderShipToKey(String headerShipToKey) {
		this.headerShipToKey = headerShipToKey;
	}

	@Override
	public String toString()
	{
			
		StringBuilder sb = new StringBuilder();
		if(convertOrder() )
		{
			sb.append(XMLTagConstants.START_TAG_ORDER);
			sb.append(SOCConstants.BLANK_STRING);
	
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.BILL_TO_ID,getBillToID()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.BILL_TO_KEY,getBillToKey()));
			sb.append(SOCConstants.BLANK_STRING);
	
//			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.BUYER_USER_ID,getBuyerUserId()));
//			sb.append(SOCConstants.BLANK_STRING);		
	
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.CREATED_AT_NODE, getCreatedAtNode()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.CUSTOMER_CONTACT_ID, getCustContactID()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.CUSTOMER_EMAIL_ID, SOCUtility.replaceXMLSpecialCharacters(getCustEmailID())));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.DOCUMENT_TYPE, getDocumentType()));
			sb.append(SOCConstants.BLANK_STRING);
	
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.ENTERED_BY, getEnteredBy()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.ENTERPRISE_CODE,getEnterpriseCode()));
			sb.append(SOCConstants.BLANK_STRING);
	
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.ORDER_COMPLETE,getOrderComplete()));
			sb.append(SOCConstants.BLANK_STRING);
	
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.ORDER_DATE, getOrderDate()));
			sb.append(SOCConstants.BLANK_STRING);
	
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.ORDER_HEADER_KEY, getOrderHeaderKey()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.ORDER_NAME, getOrderName()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.ORDER_NUM, getOrderNum()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.ORG_TAX, getOrgTax()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.ORG_TOTAL_AMT, getOrgTotalAmt()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.OTHER_CHARGES, getOtherCharges()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.PURGE_HIS_DATE, getPurgeHisDate()));
			sb.append(SOCConstants.BLANK_STRING);
	
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.SELLER_ORG_CODE, getSellerOrgCode()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.SHIP_NODE, getShipNode()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.SHIP_TO_ID, getShipToID()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.SHIP_TO_KEY,getShipToKey()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.TAX_EXEMPT, getTaxExempt()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.TAX_JURISDICTION, getTaxJurisdiction()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCUtility.getAttribute(XMLAttributeConstants.VENDOR_ID, getVendorID()));
			sb.append(SOCConstants.BLANK_STRING);
			
			sb.append(SOCConstants.CLOSING_XML_TAG);
			
			String orderExtn=getOrderExtn().toString();
			if(orderExtn.length()>0)
			{
				sb.append(SOCConstants.NEW_LINE);
				sb.append(orderExtn);
			}
			
			String orderPriceInfo = getOrderPriceInfo().toString();
			if(orderPriceInfo.length()>0)
			{
				sb.append(SOCConstants.NEW_LINE);
				sb.append(orderPriceInfo);
			}
			
//			String infoBillTo = getInfoBillTo().toString();
//			if(infoBillTo.length()>0)
//			{
//				sb.append(SOCConstants.NEW_LINE);
//				sb.append(infoBillTo);
//			}
			
			String infoShipTo = getInfoShipTo().toString();
			if(infoShipTo.length()>0)
			{
				sb.append(SOCConstants.NEW_LINE);
				sb.append(infoShipTo);
			}
			
//			String infoSoldTo = getInfoSoldTo().toString();
//			if(infoSoldTo.length()>0)
//			{
//				sb.append(SOCConstants.NEW_LINE);
//				sb.append(infoSoldTo);
//			}
			
			String orderNotes=getOrderNotes().toString();
			if(orderNotes.length()>0)
			{
				sb.append(SOCConstants.NEW_LINE);
				sb.append(orderNotes);
			}
			
			String headerCharges = getHeaderCharges().toString();
			if(headerCharges.length()>0)
			{
				sb.append(SOCConstants.NEW_LINE);
				sb.append(headerCharges);
			}
			
			String orderLines=getOrderLines().toString();
			if(orderLines.length()>0)
			{
				sb.append(SOCConstants.NEW_LINE);
				sb.append(orderLines);
			}
	
			
			sb.append(SOCConstants.NEW_LINE);
			sb.append(XMLTagConstants.END_TAG_ORDER);

		}

		return sb.toString();
	}
	
	public void setHeaderChargeTotal()
	{
		if(headerCharges!= null  ){
			totalHeaderCharge = headerCharges.getHeaderChargeTotal();
		}
		
		otherCharges= totalHeaderCharge.toString();
	}
	
	public void setTotalTax()
	{
		if(orderLines!=null )
		{
			List<SOCOrderLine> orderLinesList = orderLines.getOrderLineList();
			if(orderLinesList!=null && orderLinesList.size()>0)
			{
				Iterator<SOCOrderLine> iter=orderLinesList.iterator();
	    		while(iter.hasNext())
		    	{
	    			SOCOrderLine line = iter.next();
	    			if(!line.getIsReturned())
	    			{
	    				totalTax = totalTax.add(line.getLineTotalTax());
	    			}
				}
	    		
			}
		}
		orgTax = totalTax.toString();
	}
	
	private boolean convertOrder()
	{
		boolean result=false;
		if(orderLines!=null )
		{
			List<SOCOrderLine> orderLinesList = orderLines.getOrderLineList();
			if(orderLinesList!=null && orderLinesList.size()>0)
			{
				result=true;	
			}			
		}
		
		return result;
	}

	public void setOrderShipToKey()
	{
		boolean shipToKeyExist = false;
		if(orderLines!=null )
		{
			List<SOCOrderLine> orderLinesList = orderLines.getOrderLineList();
			if(orderLinesList!=null && orderLinesList.size()>0)
			{
				Iterator<SOCOrderLine> iter=orderLinesList.iterator();
	    		while(iter.hasNext())
		    	{
	    			SOCOrderLine line = iter.next();
	    			String deliveryMethod = line.getDeliveryMethod();
	    			if("DEL".equals(deliveryMethod))
	    			{
	    				shipToKeyExist=true;
	    				break;
	    			}
				}	    		
			}
		}
		if(shipToKeyExist)
		{
//			shipToKey = headerShipToOrgId;
			shipToKey = headerShipToKey;
		}
	}
	
	public void setTotalLines()
	{
		if(orderLines!=null )
		{
			List<SOCOrderLine> orderLinesList = orderLines.getOrderLineList();
			if(orderLinesList!=null && orderLinesList.size()>0)
			{
				Iterator<SOCOrderLine> iter=orderLinesList.iterator();
	    		while(iter.hasNext())
		    	{
	    			SOCOrderLine line = iter.next();
	    			if(!line.getIsReturned())
	    			{
	    				totalLine = totalLine.add(line.getLineTotal());
	    			}
				}	    		
			}
		}
	}
	
	public void setOrderTotal()
	{
		BigDecimal totalOrder = totalHeaderCharge.add(totalLine);
		
		orgTotalAmt = totalOrder.toString();
	}
}
